//
//  AddDeviceViewController.m
//  ZhongKeXinHeater
//
//  Created by Kendama on 2017/6/2.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "AddDeviceViewController.h"
#import <SystemConfiguration/CaptiveNetwork.h>


@interface AddDeviceViewController ()

/** <#Description#> **/
@property(nonatomic,strong) UIImageView *stateIV;

/** 连接界面 **/
@property(nonatomic,strong) UIView *connectView;
@property(nonatomic,strong) UILabel *connectLabel;
@property(nonatomic,strong) UILabel *connectTipsLabel;
@property(nonatomic,strong) UIButton *nextButton;
@property(nonatomic,strong) UITextField *nameTF;
@property(nonatomic,strong) UITextField *passwordTF;

/** 连接失败界面 **/
@property(nonatomic,strong) UIView *failConnectView;
@property(nonatomic,strong) UILabel *failLabel;
@property(nonatomic,strong) UILabel *failTipsLabel;
@property(nonatomic,strong) UILabel *suggestionLabel;
@property(nonatomic,strong) UIButton *againButton;


@end

@implementation AddDeviceViewController
- (void)dealloc{
    NSLog(@"");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self fetchNetInfo];
}



- (void)refreshBaseControlValue
{
    
    [self setNavigationTitle:@"设备联网" titleColor:[UIColor blackColor]];
    self.navigationBar.backgroundColor = [UIColor colorWithHexString:@"D0D0D0"];
    [self setLeftButtonImage:[UIImage imageNamed:@"nav_icon_back"]];
    
    [self masLayoutSubviews];
}


#pragma mark -- 事件
- (void)goToErrroShow{
    [self.view bringSubviewToFront:self.failConnectView];
    self.stateIV.image = [UIImage imageNamed:@"icon_network"];
}

- (NSDictionary *)fetchNetInfo
{
    NSArray *interfaceNames = CFBridgingRelease(CNCopySupportedInterfaces());
    NSLog(@"%s: Supported interfaces: %@", __func__, interfaceNames);
    
    NSDictionary *SSIDInfo;
    for (NSString *interfaceName in interfaceNames) {
        SSIDInfo = CFBridgingRelease(CNCopyCurrentNetworkInfo((__bridge CFStringRef)interfaceName));
        
        BOOL isNotEmpty = (SSIDInfo.count > 0);
        if (isNotEmpty) {
            break;
        }
    }
    NSString *ssid = [SSIDInfo objectForKey:@"SSID"] ;
    
    if(ssid)
    {
        NSString *ssids=[NSString stringWithFormat:@"%@",ssid];
        self.nameTF.text = ssids;
        
        NSLog(@"ssidpass==%@",DTCEXRACT_OBJECT(ssid));
        if (DTCEXRACT_OBJECT(ssid)!=nil) {
            self.passwordTF.text = DTCEXRACT_OBJECT(ssid);
        }else{
            self.passwordTF.text = @"";
        }
        
    }else{
        
    }
    return SSIDInfo;
}

#pragma mark -- 点击事件
- (void)didClickNextButton {
    
    if (!(self.nameTF.text.length != 0 && self.passwordTF.text.length != 0)) {
        [SVProgressHUD showInfoWithStatus:@"请输入正确的WiFi密码" maskType:SVProgressHUDMaskTypeBlack];
        return;
    }

    DTCSAVE_OBJECT(self.passwordTF.text, self.nameTF.text);
    [SVProgressHUD showWithStatus:@"联网中" maskType:SVProgressHUDMaskTypeBlack];
    [[DTCloudManager defaultJNI_iOS_SDK]startDeviceMatchingNetwork:LXLinkMan deviceProductId:TYPEID       deviceName:@"WiFi取暖器" wifiSSID:self.nameTF.text wifiPassword:self.passwordTF.text successCallback:^(NSDictionary *dic) {
        
        [SVProgressHUD dismiss];
        DTDevice *device = dic[@"data"];
        
        [[DTCloudManager defaultJNI_iOS_SDK]bindDeviceByName:device.deviceName macAddress:device.macAddress productId:device.deviceTypeID deviceType:DeviceUsedByWiFi successCallback:^(NSDictionary *dic) {
            [self.navigationController popToRootViewControllerAnimated:YES];
           
        } errorCallback:^(NSDictionary *dic) {
            [SVProgressHUD showInfoWithStatus:dic[@"errmsg"] maskType:SVProgressHUDMaskTypeBlack];
            [self goToErrroShow];
        }];
        NSLog(@"%@",dic);
    } errorCallback:^(NSDictionary *dic) {
        [SVProgressHUD dismiss];
        NSLog(@"%@",dic);
        [SVProgressHUD showInfoWithStatus:dic[@"errmsg"] maskType:SVProgressHUDMaskTypeBlack];
        [self goToErrroShow];

    }];

}


- (void)didClickAgainConnectButton {
    [self.view bringSubviewToFront:self.connectView];
    self.stateIV.image = [UIImage imageNamed:@"icon_network1"];
}



#pragma mark -- 懒加载
- (UIImageView *)stateIV
{
    if(!_stateIV)
    {
        UIView *superView = self.view;
        _stateIV = [[UIImageView alloc]init];
        [superView addSubview:_stateIV];
        _stateIV.image = [UIImage imageNamed:@"icon_network1"]; // icon_network
    }
    return _stateIV;
}

- (UIView *)connectView
{
    if(!_connectView)
    {
        UIView *superView = self.view;
        _connectView = [[UIView alloc]init];
        [superView addSubview:_connectView];
        _connectView.backgroundColor = [UIColor whiteColor];
    }
    return _connectView;
}

- (UILabel *)connectLabel
{
    if(!_connectLabel)
    {
        UIView *superView = self.connectView;
        _connectLabel = [[UILabel alloc]init];
        [superView addSubview:_connectLabel];
        _connectLabel.textAlignment = NSTextAlignmentCenter;
        [_connectLabel setFont:[UIFont systemFontOfSize:px1080Width(36)]];
        [_connectLabel setTextColor:[UIColor colorWithHexString:@"#1E1D1C"]];
        _connectLabel.text = @"请确认连接的无线网络并输入密码";
    }
    return _connectLabel;
}

- (UILabel *)connectTipsLabel
{
    if(!_connectTipsLabel)
    {
        UIView *superView = self.connectView;
        _connectTipsLabel = [[UILabel alloc]init];
        [superView addSubview:_connectTipsLabel];
        _connectTipsLabel.numberOfLines = 0;
        [_connectTipsLabel setFont:[UIFont systemFontOfSize:px1080Width(39)]];
        [_connectTipsLabel setTextColor:[UIColor colorWithHexString:@"#6F6F6F"]];
        _connectTipsLabel.text = @"1、在开始连接之前，长按设备S键3秒，设备按键背光闪烁，表示设备进入配对模式。\n\n2、输入连接无线网络的密码，开始连接。";
    }
    return _connectTipsLabel;
}

- (UITextField *)nameTF
{
    if(!_nameTF)
    {
        UIView *superView = self.connectView;
        _nameTF = [[UITextField alloc]init];
        _nameTF.font = [UIFont systemFontOfSize:px1080Width(42)];
        _nameTF.textColor = [UIColor colorWithHexString:@"#333333"];
        _nameTF.placeholder = @"请输入WIFI名称";
        [superView addSubview:_nameTF];
    }
    return _nameTF;
}

- (UITextField *)passwordTF
{
    if(!_passwordTF)
    {
        UIView *superView = self.connectView;
        _passwordTF = [[UITextField alloc]init];
        _passwordTF.font = [UIFont systemFontOfSize:px1080Width(42)];
        _passwordTF.textColor = [UIColor colorWithHexString:@"#333333"];
        _passwordTF.secureTextEntry = YES;
        _passwordTF.placeholder = @"请输入WIFI密码";
        [superView addSubview:_passwordTF];
    }
    return _passwordTF;
}

- (UIButton *)nextButton
{
    if(!_nextButton)
    {
        _nextButton = [[UIButton alloc]init];
        [_nextButton setTitle:@"下一步" forState:UIControlStateNormal];
        [_nextButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_nextButton addTarget:self action:@selector(didClickNextButton) forControlEvents:UIControlEventTouchUpInside];
        _nextButton.layer.cornerRadius = 20;
        _nextButton.backgroundColor = [UIColor colorWithHexString:@"#D0D0D0"];
        [self.connectView addSubview:_nextButton];
    }
    return _nextButton;
}

- (UIView *)failConnectView
{
    if(!_failConnectView)
    {
        UIView *superView = self.view;
        _failConnectView = [[UIView alloc]init];
        [superView addSubview:_failConnectView];
        _failConnectView.backgroundColor = [UIColor whiteColor];
    }
    return _failConnectView;
}

- (UIButton *)againButton
{
    if(!_againButton)
    {
        _againButton = [[UIButton alloc]init];
        [_againButton setTitle:@"重新连接" forState:UIControlStateNormal];
        [_againButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_againButton addTarget:self action:@selector(didClickAgainConnectButton) forControlEvents:UIControlEventTouchUpInside];
        _againButton.layer.cornerRadius = 20;
        _againButton.backgroundColor = [UIColor colorWithHexString:@"#D0D0D0"];
        [self.failConnectView addSubview:_againButton];
    }
    return _againButton;
}

- (UILabel *)failLabel
{
    if(!_failLabel)
    {
        UIView *superView = self.failConnectView;
        _failLabel = [[UILabel alloc]init];
        [superView addSubview:_failLabel];
        _failLabel.textAlignment = NSTextAlignmentCenter;
        [_failLabel setFont:[UIFont boldSystemFontOfSize:px1080Width(52)]];
        [_failLabel setTextColor:[UIColor colorWithHexString:@"#231916"]];
        _failLabel.text = @"很遗憾，未能成功连接设备！";
    }
    return _failLabel;
}
- (UILabel *)failTipsLabel
{
    if(!_failTipsLabel)
    {
        UIView *superView = self.failConnectView;
        _failTipsLabel = [[UILabel alloc]init];
        [superView addSubview:_failTipsLabel];
        _failTipsLabel.numberOfLines = 0;
        _failTipsLabel.textAlignment = NSTextAlignmentCenter;
        [_failTipsLabel setFont:[UIFont systemFontOfSize:px1080Width(45)]];
        [_failTipsLabel setTextColor:[UIColor colorWithHexString:@"#A4A4A4"]];
        _failTipsLabel.text = @"连接失败的可能原因如下，请按照指引尝试重新连接。";
    }
    return _failTipsLabel;
}
- (UILabel *)suggestionLabel
{
    if(!_suggestionLabel)
    {
        UIView *superView = self.failConnectView;
        _suggestionLabel = [[UILabel alloc]init];
        [superView addSubview:_suggestionLabel];
        _suggestionLabel.numberOfLines = 0;
        [_suggestionLabel setFont:[UIFont systemFontOfSize:px1080Width(45)]];
        [_suggestionLabel setTextColor:[UIColor colorWithHexString:@"#6F6F6F"]];
        _suggestionLabel.text = @"1、设备未进入可连接状态，请调试设备使其进入可配对状态，然后尝试重新连接。\n\n2、手机未与设备连接到同一WIFI信号。\n\n3、WIFI信号不稳定，请尝试重启路由器解决。";
    }
    return _suggestionLabel;
}

#pragma mark -- 初始化
- (void)masLayoutSubviews{
    __weak UIView *superView = self.view;
    [self.stateIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(px1920Height(262));
        make.centerX.mas_equalTo(superView.mas_centerX);
        make.width.mas_equalTo(px1080Width(260));
        make.height.mas_equalTo(px1920Height(260));
        
    }];
    
    [self.failConnectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.stateIV.mas_bottom);
        make.left.right.bottom.equalTo(superView);
    }];
    
    [self.failLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.failConnectView).with.offset(px1920Height(115));
        make.left.right.mas_equalTo(self.failConnectView);
        make.height.mas_equalTo(px1080Width(73));
    }];
    
    [self.failTipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.failLabel.mas_bottom).with.offset(px1920Height(115));
        make.left.mas_equalTo(self.failConnectView).with.offset(px1080Width(113));
        make.right.mas_equalTo(self.failConnectView).with.offset(-px1080Width(113));
        make.height.mas_equalTo(px1920Height(126));
    }];
    
    UIView *suggestionView = [[UIView alloc] init];
    [self.failConnectView addSubview:suggestionView];
    suggestionView.layer.cornerRadius = 5;
    suggestionView.layer.borderWidth = 0.5;
    [suggestionView.layer setMasksToBounds:YES];
    suggestionView.backgroundColor = [UIColor colorWithHexString:@"F9F9F9"];
    
    [suggestionView addSubview:self.suggestionLabel];
    [suggestionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.failTipsLabel.mas_bottom).with.offset(px1920Height(72));
        make.left.mas_equalTo(self.failConnectView).with.offset(px1080Width(55));
        make.right.mas_equalTo(self.failConnectView).with.offset(-px1080Width(55));
        make.height.mas_equalTo(px1920Height(450));
    }];
    
    [self.suggestionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(suggestionView).with.offset(px1920Height(18));
        make.left.mas_equalTo(suggestionView).with.offset(px1080Width(36));
        make.right.mas_equalTo(suggestionView).with.offset(-px1080Width(36));
        make.bottom.equalTo(suggestionView).with.offset(-px1920Height(18));
    }];
    
    [self.againButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(suggestionView.mas_bottom).with.offset(px1920Height(170));
        make.centerX.mas_equalTo(superView.mas_centerX);
        make.width.mas_equalTo(px1080Width(500));
        make.height.mas_equalTo(px1920Height(136));
    }];
    
    
    [self.connectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.stateIV.mas_bottom);
        make.left.right.bottom.equalTo(superView);
    }];
 
    [self.connectLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.connectView.mas_top).with.offset(px1080Width(75));
        make.left.right.equalTo(self.connectView);
        make.height.mas_equalTo(px1920Height(50));
    }];
    
    int count = 2;
    UIView *lastView = nil;
    NSArray *array = @[self.nameTF,self.passwordTF];
    
    for (int i = 0; i < count; i++) {
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor whiteColor];
        view.layer.cornerRadius = 5;
        view.layer.borderWidth = 0.5;
        [view.layer setMasksToBounds:YES];
        [self.connectView addSubview:view];
        
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).with.offset(px1080Width(60));
            make.right.equalTo(self.view).with.offset(-px1080Width(60));
            make.height.mas_equalTo(px1920Height(136));
            
            //判断是否有前一个子View
            if ( lastView ) {
                //如果有前一个View，上边界和前一个View的下边界紧贴
                make.top.mas_equalTo(lastView.mas_bottom).with.offset(px1920Height(20));
            } else {
                //如果没有前一个View，上边界和container的下边界紧贴
                make.top.mas_equalTo(self.connectLabel.mas_bottom).with.offset(px1920Height(20));
            }
        }];
        
        UITextField *textField = array[i];
        [view addSubview:textField];
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(view);
            make.left.equalTo(view).with.offset(px1080Width(58));
            make.right.equalTo(view).with.offset(-px1080Width(58));
            make.height.mas_equalTo(px1920Height(100));
        }];
        lastView = view;
    }
    
    
    [self.nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lastView.mas_bottom).with.offset(px1920Height(120));
        make.centerX.mas_equalTo(superView.mas_centerX);
        make.width.mas_equalTo(px1080Width(500));
        make.height.mas_equalTo(px1920Height(136));
    }];
    
    [self.connectTipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nextButton.mas_bottom).with.offset(px1920Height(105));
        make.left.mas_equalTo(self.connectView).with.offset(px1080Width(76));
        make.right.mas_equalTo(self.connectView).with.offset(-px1080Width(76));
        make.height.mas_equalTo(px1920Height(400));
        
    }];
    
}

@end
